<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\CommonEnum;
use app\model\KsMaterialDailyDataModel;
use app\model\mongo\MongoKsAccountDayOldReportModel;
use app\model\mongo\MongoKsVideoDayReportModel;
use app\model\mongo\MongoOeAccountDayOldReportModel;
use app\model\mongo\MongoOeVideoDayReportModel;
use app\model\mongo\MongoQqPicDayOldReportModel;
use app\model\mongo\MongoQqPicModel;
use app\model\mongo\MongoQqVideoDayReportModel;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use think\facade\Db;

class TestHistoryCostStatWork extends BaseWork
{
    public array $accountList = [];

    public array $videoList = [];

    protected function configure()
    {
        // 指令配置
        $this->setName('test_history_cost_stat_work');
        // 设置参数
        $this->setDescription('test-添加历史消耗数据')
            ->addArgument('media_id', Argument::REQUIRED, '媒体id')
            ->setHelp('php think test_repair_append_cost_work');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $mediaId = intval($input->getArgument('media_id'));
        $statDateInt = 20230301;
        $this->getOutputMessage('历史记录添加开始');

        if ($mediaId == CommonEnum::QQ_MEDIA) {
            $list = $this->_getQqHistoryCostList($statDateInt);
            $res = $this->_insertQqData($list);
        } elseif ($mediaId == CommonEnum::OE_MEDIA) {
            $list = $this->_getOeHistoryCostList($statDateInt);
            $res = $this->_insertOeData($list);
        } else {
            $list = $this->_getKsHistoryCostList($statDateInt);
            $res = $this->_insertKsData($list);
        }

        $this->getOutputMessage('历史记录添加结束');
    }

    private function _insertQqData(array $list = []): bool
    {
        $res = (new MongoQqPicDayOldReportModel())->insertAll($list, 10000);
        if ($res <= 0) {
            return false;
        }
        return true;
    }

    private function _insertKsData(array $list = []): bool
    {
        $res = (new MongoKsAccountDayOldReportModel())->insertAll($list, 10000);

        if ($res <= 0) {
            return false;
        }
        return true;
    }

    private function _insertOeData(array $list = []): bool
    {
        $res = (new MongoOeAccountDayOldReportModel())->insertAll($list, 10000);
        if ($res <= 0) {
            return false;
        }
        return true;
    }

    private function _updateSync(int $id = 0): bool
    {
        $res = (new KsMaterialDailyDataModel())->where('id', $id)->update(['is_sync' => 1]);

        if ($res <= 0) {
            return false;
        }

        return true;
    }

    private function _getKsInfo(array $params = [])
    {
        return (new MongoKsVideoDayReportModel())->where([
            ['account_id', '=', $params['account_id']],
            ['stat_date', '=', $params['stat_date']],
            ['video_id', '=', $params['video_id']],
        ])->find();
    }

    private function _getQqInfo(array $params = [])
    {
        return (new MongoQqVideoDayReportModel())->where([
            ['account_id', '=', $params['account_id']],
            ['stat_date', '=', $params['stat_date']],
            ['video_id', '=', $params['video_id']],
        ])->find();
    }

    private function _getOeInfo(array $params = [])
    {
        return (new MongoOeVideoDayReportModel())->where([
            ['account_id', '=', $params['account_id']],
            ['stat_date', '=', $params['stat_date']],
            ['video_id', '=', $params['video_id']],
            ['source', '=', $params['source']],
        ])->find();
    }

    private function _getOeHistoryCostList(int $statDate = 0): array
    {
        $res = Db::name('oe_account_stat')->field(true)->select()->toArray();
        foreach ($res as $k => $v) {
            $res[$k]['cost'] = floatval($v['cost']);
            $res[$k]['cash_cost'] = floatval($v['cash_cost']);
            $res[$k]['balance'] = floatval($v['balance']);
            $res[$k]['frozen'] = floatval($v['frozen']);
            $res[$k]['income'] = floatval($v['income']);
            $res[$k]['reward_cost'] = floatval($v['reward_cost']);
            $res[$k]['shared_wallet_cost'] = floatval($v['shared_wallet_cost']);
            $res[$k]['transfer_in'] = floatval($v['transfer_in']);
            $res[$k]['transfer_out'] = floatval($v['transfer_out']);
            $res[$k]['stat_date'] = $statDate;
            $res[$k]['create_time'] = time();
            $res[$k]['update_time'] = time();
            unset($res[$k]['id']);
        }
        return $res;
    }

    private function _getKsHistoryCostList(int $statDate = 0): array
    {
        $res = Db::name('ks_account_stat')->field(true)->select()->toArray();
        foreach ($res as $k => $v) {
            $res[$k]['daily_charge'] = floatval($v['daily_charge']);
            $res[$k]['real_charged'] = floatval($v['real_charged']);
            $res[$k]['contract_rebate_real_charged'] = floatval($v['contract_rebate_real_charged']);
            $res[$k]['balance'] = floatval($v['balance']);
            $res[$k]['direct_rebate_real_charged'] = floatval($v['direct_rebate_real_charged']);
            $res[$k]['daily_transfer_in'] = floatval($v['daily_transfer_in']);
            $res[$k]['real_recharged'] = floatval($v['real_recharged']);
            $res[$k]['contract_rebate_real_recharged'] = floatval($v['contract_rebate_real_recharged']);
            $res[$k]['direct_rebate_real_recharged'] = floatval($v['direct_rebate_real_recharged']);
            $res[$k]['daily_transfer_out'] = floatval($v['daily_transfer_out']);
            $res[$k]['stat_date'] = $statDate;
            $res[$k]['create_time'] = time();
            $res[$k]['update_time'] = time();
            unset($res[$k]['id'],$res[$k]['image_url']);
        }
        return $res;
    }

    private function _getQqHistoryCostList(int $statDate = 0): array
    {
        $res = Db::name('qq_pic_stat')->field(true)->select()->toArray();
        $picList = (new MongoQqPicModel())->whereIn('image_id', array_column($res, 'pic_id'))->column('signature', 'image_id');

        foreach ($res as $k => $v) {
            $res[$k]['cost'] = floatval($v['cost']);
            $res[$k]['stat_date'] = $statDate;
            $res[$k]['signature'] = $picList[$v['pic_id']] ?? '';
            $res[$k]['create_time'] = time();
            $res[$k]['update_time'] = time();
            unset($res[$k]['id']);
        }
        return $res;
    }
}
